﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BorrowBooksService : IBorrowBooksService
    {
        private IBorrowBooksDal _borrowBooks;
        private IMapper _mapper;
        public BorrowBooksService(IBorrowBooksDal borrowBooks, IMapper mapper)
        {
            _borrowBooks = borrowBooks;
            _mapper = mapper;
        }
        public async Task<int> AddBorrowBooksAsync(int postponeApplyRecord, int bookInfoId, int borrowPerson, int borrowState, int borrowStandardId, int PostponeTime)
        {
            return await _borrowBooks.AddAsync(new Borrowbook 
            {
                PostponeApplyRecord = postponeApplyRecord,
                BooksInfoId = bookInfoId,
                BorrowPerson = borrowPerson,
                BorrowState = borrowState,
                BorrowStandardId = borrowStandardId,
                PostponeTime = PostponeTime
            });
        }

        public async Task<int> DeleteBorrowBooksAsync(int id)
        {
            var data = await _borrowBooks.QueryAsync(id);
            return data == null ? -1 : await _borrowBooks.DeleteAsync(data);
        }

        public async Task<int> EditBorrowBooksAsync(int id, int PostponeApplyRecord, int bookInfoId, int borrowPerson, int borrowState, int borrowStandardId, int PostponeTime)
        {
            var data = await _borrowBooks.QueryAsync(id);
            if (data == null)
                return -1;
            data.PostponeApplyRecord = PostponeApplyRecord;
            data.BooksInfoId = bookInfoId;
            data.BorrowPerson = borrowPerson;
            data.BorrowState = borrowState;
            data.BorrowStandardId = borrowStandardId;
            data.PostponeTime = PostponeTime;
            return await _borrowBooks.EditAsync(data);
        }

        public async  Task<List<BorrowBooksDto>> QueryBorrowBooksAllAsync()
        {
            var data = await _borrowBooks.QueryAsync();
            return _mapper.Map<List<Borrowbook>, List<BorrowBooksDto>>(data);
        }

        public async Task<BorrowBooksDto> QueryBorrowBooksByIdAsync(int id)
        {
            var data = await _borrowBooks.QueryAsync(id);
            return _mapper.Map<Borrowbook, BorrowBooksDto>(data);
        }
    }
}
